/*
 * Copyright [2011] [Henry and Will]

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 * */
package org.hnw.wte;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.widget.Toast;

public class wteDataBase {
	
	private Context context;
	private SQLiteDatabase database;
	private wteDbHelper dbhelper;
	
	//define the column names for the table
	
	final public String KEY_ID = "_id";
	final public String KEY_NAME = "name";
	final public String KEY_DES = "description";
	final public String KEY_ADD = "address";
	final public String KEY_IMGE = "image";
	
	//constructor
	public wteDataBase(Context context){
		this.context = context;
	}
	
	//method to open the database
	public wteDataBase DBopen(){
		
		dbhelper = new wteDbHelper(context); 
		database = dbhelper.getWritableDatabase();
		return this;
	}
	
	//method to close the database
	public void DBclose(){
		database.close();
	}
	
	//the following three methods are meant to edit entries in the table
	
	//method to insert the tuples
	public long DBinsert(Restaurant restaurant){
		long res = 0;
		
		ContentValues insertValue = createContentValues(restaurant);

		res = database.insert("wte_restaurant", null, insertValue);
		
		return res;
	}
	
	//method to update the tuples
	public boolean DBupdate(Restaurant restaurant){
		
		long res = 0;
		
		ContentValues updateValue = createContentValues(restaurant);
		
		res = database.update("wte_restaurant", updateValue, "_id="+restaurant.getID(), null);
		
		return res>0;
		
	}
	
	//method to remove the tuples
	public boolean DBremove(long rowId){
		long res = 0;
		
		res = database.delete("wte_restaurant", "_id="+rowId, null);
		
		return res>0;
	}
	
	//the following two methods return restaurant tuples which contains result tuples
	
	//method to retrieve all tuples
	public ArrayList<Restaurant> DBretrieveAll(){
		Cursor Acursor = null;
		
		try{
			Acursor = database.query("wte_restaurant", null, null, null, null, null, null);
		}catch(SQLiteException e){
			Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
		}
		
		ArrayList<Restaurant> res_list = new ArrayList<Restaurant>();
		
		if(Acursor.getCount()==0 && res_list.isEmpty())
			return res_list;
		
		Acursor.moveToFirst();
		Restaurant first_ele = new Restaurant(
				Acursor.getLong(Acursor.getColumnIndex(this.KEY_ID)),
				Acursor.getString(Acursor.getColumnIndex(this.KEY_NAME)),
				Acursor.getString(Acursor.getColumnIndex(this.KEY_DES)),
				Acursor.getString(Acursor.getColumnIndex(this.KEY_ADD)),
				Acursor.getString(Acursor.getColumnIndex(this.KEY_IMGE)));
		res_list.add(first_ele);
		
		while(Acursor.moveToNext()){
			Restaurant ele = new Restaurant(
					Acursor.getLong(Acursor.getColumnIndex(this.KEY_ID)),
					Acursor.getString(Acursor.getColumnIndex(this.KEY_NAME)),
					Acursor.getString(Acursor.getColumnIndex(this.KEY_DES)),
					Acursor.getString(Acursor.getColumnIndex(this.KEY_ADD)),
					Acursor.getString(Acursor.getColumnIndex(this.KEY_IMGE)));
			res_list.add(ele);
		}
		return res_list;
		
	}
	
	//method to retrieve single tuples
	public Restaurant DBretrieveOne(long rowId) throws SQLException{
		Cursor Scursor = null;
		
		try{
			Scursor =database.query("wte_restaurant", null, "_id="+rowId, null, null, null, null);
		}catch(SQLiteException e){
			
		}
		
		if(Scursor.getCount() == 0)
			return new Restaurant();
		else
			Scursor.moveToFirst();
		
		Restaurant ele = new Restaurant(
				Scursor.getLong(Scursor.getColumnIndex(this.KEY_ID)),
				Scursor.getString(Scursor.getColumnIndex(this.KEY_NAME)),
				Scursor.getString(Scursor.getColumnIndex(this.KEY_DES)),
				Scursor.getString(Scursor.getColumnIndex(this.KEY_ADD)),
				Scursor.getString(Scursor.getColumnIndex(this.KEY_IMGE)));
		
		return ele;
	}
	
	//method to package all information
	private ContentValues createContentValues(Restaurant restaurant){
		ContentValues values = new ContentValues();
		
		values.put(this.KEY_NAME, restaurant.getName());
		values.put(this.KEY_ADD, restaurant.getAddress());
		values.put(this.KEY_DES, restaurant.getDescription());
		values.put(this.KEY_IMGE, restaurant.getImage());
		return values;
	}
}
